package com.local.user.controller;

import com.local.common.util.PageResultUtil;
import com.local.common.util.ResultUtil;
import com.local.user.pojo.User;
import com.local.user.pojo.UserListRequestParam;
import com.local.user.pojo.UserLockRequestParam;
import com.local.user.pojo.UserLoginRequestParam;
import com.local.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * user列表数据接口，包括翻页（后台）
     * @param requestParam
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultUtil list(@RequestBody UserListRequestParam requestParam) {
        Integer page = requestParam.getPage();
        page = Objects.isNull(page) || page <= 0 ? 1 : page;
        Integer limit = requestParam.getLimit();
        limit = Objects.isNull(limit) || limit <= 0 ? 10 : limit;
        String sort = requestParam.getSort();
        sort = Objects.isNull(sort) || sort.isEmpty() ? "user_id" : sort;
        String order = requestParam.getOrder();
        order = Objects.isNull(order) || order.isEmpty() ? "desc" : order;
        String userName = requestParam.getUserName();
        Integer lockedFlag = requestParam.getLockedFlag();
        Integer isDeleted = requestParam.getIsDeleted();

        PageResultUtil userList = userService.getUserList(page, limit, sort, order, userName, isDeleted, lockedFlag);
        if (Objects.nonNull(userList)) {
            return new ResultUtil(200, "", userList);
        } else {
            return new ResultUtil(201, "请求失败，请稍后再试");
        }
    }


    /**
     * 锁定/解锁用户状态（后台）
     * @param requestParam
     * @return
     */
    @PostMapping("/lock")
    @ResponseBody
    public ResultUtil lock(@RequestBody UserLockRequestParam requestParam) {
        Integer[] ids = requestParam.getIds();
        Integer lockedFlag = requestParam.getLockedFlag();
        Integer result = userService.updateLockedFlag(ids, lockedFlag);
        String message = lockedFlag == 1 ? "解禁" : "禁用";
        if (result > 0) {
            return new ResultUtil(200, message + "成功");
        } else {
            return new ResultUtil(201, message + "失败，请稍后再试");
        }
    }


    /**
     * 通过用户名和密码查询用户，用于前台登录
     * @param requestParam
     * @return
     */
    @GetMapping("/getByLoginNamePassword")
    @ResponseBody
    public ResultUtil getByLoginNamePassword(@RequestBody UserLoginRequestParam requestParam) {
        String loginName = requestParam.getLoginName();
        String password = requestParam.getPassword();
        if (Objects.isNull(loginName) || loginName.isEmpty() || Objects.isNull(password) || password.isEmpty()) {
            return new ResultUtil(201, "请输入登录名和密码");
        }

        User user = userService.getByLoginNamePassword(loginName, password);
        if (Objects.nonNull(user)) {
            return new ResultUtil(200, "", user);
        } else {
            return new ResultUtil(201, "登录失败，请稍后再试");
        }
    }


    /**
     * 在订单核对页面中，修改地址
     * @param user
     * @return
     */
    @PostMapping("/updateAddress")
    @ResponseBody
    public ResultUtil updateAddress(@RequestBody User user) {
        Integer result = userService.updateAddress(user);
        if (result > 0) {
            return new ResultUtil(200, "修改成功");
        } else {
            return new ResultUtil(201, "修改失败，请稍后再试");
        }
    }
}
